
from .base import *
from lib.variables import *

class IndexHandler(BaseHandler):

    def get(self, *args, **kwargs):
        user_info = self.current_user
        page = int(self.get_argument("p", "1"))
        template_variables["user_info"] = user_info
        if(user_info):
            template_variables["user_info"]["counter"] = {
                "topics": '',
                "replies": '',
                "favorites": '',
            }

            template_variables["notifications_count"] = '';

        template_variables["status_counter"] = {
            "users": self.user_model.get_all_users_count(),
        }

        template_variables["active_page"] = "topic"
        template_variables["gen_random"] = gen_random
        self.render("topic/index.html", **template_variables)


class TopicsHandler(BaseHandler):
    def get(self, *args, **kwargs):
        page = int(self.get_argument("p", 1))

        # return self.write(page)
        template_variables["topic_list"] = self.topics_model.get_topics_all(num=2,current_page=page)

        self.render("topic/topics.html", **template_variables)


class NewHandler(BaseHandler):
    def get(self, *args, **kwargs):
        self.render('topic/new.html',**kwargs)


    def post(self, *args, **kwargs):
        title = self.get_argument('title', '')
        content = self.get_argument('content', '')
        node_id = self.get_argument('node_id', '')
        author_id = self.current_user['uid']

        info = {
            'title': title,
            'content': content,
            'status': 1,
            'author_id': author_id,
            'created': time.strftime('%Y-%m-%d %H:%M:%S'),
            'node_id': node_id,
        }

        self.topics_model.add_new_topics(info)
        self.redirect('/')


class DetailHandler(BaseHandler):
    def get(self, topic_id, **kwargs):
        template_variables['detail'] = detail = self.topics_model.get_detail(topic_id)
        self.render("topic/detail.html", **template_variables)